package com.sky.controller.user;

import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.user.OrdersService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
//提供用户下单方法
@Slf4j
@RestController("userOrderController")
@RequestMapping("/user")
public class OrderController {
    @Autowired
    private OrdersService ordersService;

    @PostMapping("/order/submit")
    public Result<OrderSubmitVO> submitOrders(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        log.info("用户下单：{}",ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = ordersService.submitOrders(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }
    //历史订单
    @GetMapping("/order/historyOrders")
    public Result<PageResult> historyOrders(Integer page, Integer pageSize, Integer status){
        log.info("查询历史订单：page={},pageSize={},status={}",page,pageSize,status);
        PageResult pageResult = ordersService.historyOrders(page,pageSize,status);
        return Result.success(pageResult);
    }

    @GetMapping("/order/orderDetail/{id}")
    public Result<OrderVO> findOrderVOById(@PathVariable Long id){
        log.info("根据订单id获取订单详情：id={}",id);
        OrderVO orderVO = ordersService.findOrdersById(id);
        return Result.success(orderVO);
    }

    @PutMapping("/order/cancel/{id}")
    public Result cancel(@PathVariable Long id){
        log.info("取消订单：id={}",id);
        ordersService.cancel(id);
        return Result.success();
    }

    /**
     * 订单支付  需要开通商家支付功能
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/order/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);

        // 这个模拟商家支付功能所需要的参数
        OrderPaymentVO orderPaymentVO = ordersService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        // 修改订单状态诶支付成功
        ordersService.paySuccess(ordersPaymentDTO.getOrderNumber());
        return Result.success(orderPaymentVO);
    }


    //查询到订单数据转为购物车数据
    @ApiOperation("再来一单")
    @PostMapping("/order/repetition/{id}")
    public Result againOne(@PathVariable Long id){
        log.info("再来一单{}",id);
        ordersService.againOne(id);
        return Result.success();
    }

    @ApiOperation("客户催单")
    @GetMapping("/order/reminder/{id}")
    public Result reminder(@PathVariable Long id){
        log.info("客户催单{}",id);
        ordersService.reminder(id);
        return Result.success();
    }


}
